package com.zhs.gccs.view.gt;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.annimon.stream.Stream;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hjq.shape.view.ShapeButton;
import com.zhs.gccs.R;
import com.zhs.gccs.adapter.GccsReportGridAdapter;
import com.zhs.gccs.adapter.GccsReportProblemGridAdapter;
import com.zhs.gccs.model.CheckBoxModel;
import com.zhs.gccs.model.GccsOfProject;
import com.zhs.gccs.model.GccsProblem;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.view.BaseView;
import com.zhs.gccs.vo.GccsStatistics;

import java.util.ArrayList;
import java.util.List;

public class GccsDataFilterView extends BaseView {
    private ShapeButton btn_reset;
    private ShapeButton btn_confirm;
    private GccsStatistics gccsStatistics;
    private RecyclerView tab_recycle_view;
    GccsReportGridAdapter mGridAdapter;
    private List<GccsOfProject> list;
    List<GccsProblem> problems;
    private RecyclerView result_recycle_view;
    GccsReportProblemGridAdapter problemGridAdapter;
    private String gccs;
    private String problem;

    public GccsDataFilterView(@NonNull Context context) {
        super(context);
    }

    public GccsDataFilterView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public GccsDataFilterView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_gccs_data_filter_view,
                this, true);
        tab_recycle_view = findViewById(R.id.tab_recycle_view);
        mGridAdapter = new GccsReportGridAdapter();
        tab_recycle_view.setLayoutManager(new GridLayoutManager(context, 3));
        tab_recycle_view.setAdapter(mGridAdapter);
        mGridAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GccsOfProject gccsOfProject = list.get(position);
                if(!gccsOfProject.isSelected()){
                    Stream.of(list).forEach(item->item.setSelected(false));
                    gccsOfProject.setSelected(true);
                }else {
                    gccsOfProject.setSelected(false);
                }
                mGridAdapter.notifyDataSetChanged();
            }
        });
        result_recycle_view = findViewById(R.id.result_recycle_view);
        problems = new ArrayList<>();
        problemGridAdapter = new GccsReportProblemGridAdapter();
        result_recycle_view.setLayoutManager(new GridLayoutManager(context, 2));
        result_recycle_view.setAdapter(problemGridAdapter);
        problemGridAdapter.setNewData(problems);
        problemGridAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GccsProblem problem = problems.get(position);
                if(!problem.isSelected()){
                    Stream.of(problems).forEach(item->item.setSelected(false));
                    problem.setSelected(true);
                }else {
                    problem.setSelected(false);
                }
                problemGridAdapter.notifyDataSetChanged();
            }
        });
        btn_reset = findViewById(R.id.btn_reset);
        btn_reset.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                Stream.of(list).forEach(item->item.setSelected(false));
                Stream.of(problems).forEach(item->item.setSelected(false));
                if(listener!=null){
                    listener.onSelected("","");
                }
            }
        });
        btn_confirm = findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                String gccs = Stream.of(list).filter(GccsOfProject::isSelected).map(GccsOfProject::getDbid).findFirstOrElse(null);
                String problem = Stream.of(problems).filter(item -> item.isSelected()).map(GccsProblem::getValue).findFirstOrElse(null);
                if(listener!=null){
                    listener.onSelected(gccs,problem);
                }
            }
        });
    }


    public void initData(GccsStatistics gccsStatistics,
                         String gccs,
                         String problem) {
        this.gccsStatistics = gccsStatistics;
        this.gccs = gccs;
        this.problem = problem;
        if(gccsStatistics.getXmGccs()!=null){
            list = Stream.of(gccsStatistics.getXmGccs()).
                    filter(i -> !"84FC98D87113C32AE050007F0100549D".equals(i.getDbid()))
                    .toList();
            Stream.of(list).forEach(item->{
                item.setSelected(item.getDbid().equals(gccs));
            });
            mGridAdapter.setNewData(list);
        }
        problems.clear();
        problems.add(new GccsProblem("合格","1","1".equals(problem)));
        problems.add(new GccsProblem("不合格","0","0".equals(problem)));
        problemGridAdapter.setNewData(problems);
    }

    private OnSelectedListener listener;

    public void setListener(OnSelectedListener listener) {
        this.listener = listener;
    }

    public interface OnSelectedListener{
        void onSelected(String gccs,String problem);
    }
}
